package com.tacc.tyun.service.impl;

import com.github.pagehelper.StringUtil;
import com.google.gson.Gson;
import com.tacc.tyun.base.BaseServiceImpl;
import com.tacc.tyun.common.exception.TYException;
import com.tacc.tyun.dao.master.SysConfigDao;
import com.tacc.tyun.model.SysConfig;
import com.tacc.tyun.service.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * @author tao cong cong
 * @create 17/7/2 下午2:40
 */
@Service("sysConfigService")
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfig> implements SysConfigService {

    @Resource
    private SysConfigDao sysConfigDao;

    @Autowired
    private void setSysConfigDao(SysConfigDao sysConfigDao){
        this.baseDao=sysConfigDao;
    }


    public String getValue(String key, String defaultValue) {
        String value = sysConfigDao.queryByKey(key);
        if(StringUtils.isNotEmpty(value)){
            return defaultValue;
        }
        return value;
    }

    @Override
    public <T>T getConfigObject(String key, Class<T> clazz) {
        String value = getValue(key, null);

        if (StringUtils.isNotEmpty(key)){
            return new Gson().fromJson(key,clazz);
        }
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new TYException("获取参数失败");
        }
    }

    @Override
    public int selectCountLikeKey(String key) {
        return sysConfigDao.selectCountLikeKey(key);
    }
}
